import { fetchMenu } from "@/http/menu";
export interface ListItem {
  name: string;
  auth?: string;

  path?: string;
  children?: ListItem[];
}

class UseMenu {
  private _LIST: ListItem[] = [];
  constructor(callback: (arg: ListItem[]) => void) {
    this.fetchList()
      .catch((err: Error) => console.error(err))
      .finally(() => callback(this.list));
  }
  get list(): ListItem[] {
    return this._LIST;
  }
  private set list(value: ListItem[]) {
    this._LIST = value;
  }
  public async fetchList() {
    const list = (await fetchMenu({})) as ListItem[];
    this.list = list;
  }
}

export const useMenu = (callback: (arg: ListItem[]) => void) => new UseMenu(callback);
